package com.xiangmo.travel.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiangmo.travel.Dto.AttractionsCommentsDto;
import com.xiangmo.travel.Dto.AttractionsCommentsInfoDto;
import com.xiangmo.travel.Dto.PageInfoDto;
import com.xiangmo.travel.Entity.AttractionsComments;
import com.xiangmo.travel.Entity.User;
import com.xiangmo.travel.Service.AttractionsCommentsService;
import com.xiangmo.travel.Service.LikeService;
import com.xiangmo.travel.mapper.AttractionsCommentsMapper;
import com.xiangmo.travel.mapper.LikeMapper;
import com.xiangmo.travel.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AttractionsCommentsServiceImpl implements AttractionsCommentsService {
    @Autowired
    private AttractionsCommentsMapper attractionsCommentsMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LikeService likeService;
    @Autowired
    private LikeMapper likeMapper;

    /**
     * 添加评论 需要初始化点赞数为零
     * @return 成功状态
     */
    @Override
    public int addComment(AttractionsCommentsDto attractionsCommentsDto) {
        AttractionsComments attractionsComments = AttractionsComments.builder()
                .userId(attractionsCommentsDto.getUserId())
                .attractionsId(attractionsCommentsDto.getAttractionsId())
                .content(attractionsCommentsDto.getContent())
                .build();
        return attractionsCommentsMapper.insert(attractionsComments);
    }

    /**
     * 根据id 查询当前文章的所有父评论 携带
     * @param id
     * @return
     */
    @Override
    public PageInfoDto<AttractionsCommentsInfoDto> getCommentByAttractionsId(Long id, int pageNum, int pageSize) {
        Page<AttractionsComments> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<AttractionsComments> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AttractionsComments::getAttractionsId, id)
                .and(i -> i.isNull(AttractionsComments::getParentId))
                .orderByDesc(AttractionsComments::getCreatedAt);
        attractionsCommentsMapper.selectPage(page, lambdaQueryWrapper);
        List<AttractionsComments> list = page.getRecords();
        List<AttractionsCommentsInfoDto> listInfo = new ArrayList<>();

        for(AttractionsComments a : list){
            User user = new User();
            AttractionsCommentsInfoDto attractionsCommentsInfoDto = new AttractionsCommentsInfoDto();
            user = userMapper.selectById(a.getUserId());
            Long count = likeService.getCountByCommentId(a.getId());
            //根据 当前的评论id查询子评论的数量
            LambdaQueryWrapper<AttractionsComments> lambdaQueryWrappers = new LambdaQueryWrapper<>();
            lambdaQueryWrappers.eq(AttractionsComments::getParentId, a.getId());
            Long commentNum = attractionsCommentsMapper.selectCount(lambdaQueryWrappers);
            attractionsCommentsInfoDto.setAttractionsId(a.getAttractionsId());
            attractionsCommentsInfoDto.setContent(a.getContent());
            attractionsCommentsInfoDto.setImage(user.getImage());
            attractionsCommentsInfoDto.setCreatedAt(a.getCreatedAt());
            attractionsCommentsInfoDto.setName(user.getName());
            attractionsCommentsInfoDto.setLikeNum(count);
            attractionsCommentsInfoDto.setUserId(a.getUserId());
            attractionsCommentsInfoDto.setCommentId(a.getId());
            attractionsCommentsInfoDto.setCommentNum(commentNum);
            listInfo.add(attractionsCommentsInfoDto);
        }
        PageInfoDto<AttractionsCommentsInfoDto> pageInfoDto = new PageInfoDto<>();
        pageInfoDto.setPages(page.getPages());
        pageInfoDto.setPageCurrent(page.getCurrent());
        pageInfoDto.setPageTotal(page.getTotal());
        pageInfoDto.setSize(page.getSize());
        pageInfoDto.setList(listInfo);
        return pageInfoDto;
    }

    @Override
    public PageInfoDto<AttractionsCommentsInfoDto> getCommentByAttractionsIdAndUserId(Long userId, Long id, int pageNum, int pageSize) {
        Page<AttractionsComments> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<AttractionsComments> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AttractionsComments::getAttractionsId, id)
                .and(i -> i.isNull(AttractionsComments::getParentId))
                .orderByDesc(AttractionsComments::getCreatedAt);
        attractionsCommentsMapper.selectPage(page, lambdaQueryWrapper);
        //封装
        List<AttractionsComments> list = page.getRecords();
        List<AttractionsCommentsInfoDto> listInfo = new ArrayList<>();
        for(AttractionsComments a : list){
            User user = new User();
            AttractionsCommentsInfoDto attractionsCommentsInfoDto = new AttractionsCommentsInfoDto();
            user = userMapper.selectById(a.getUserId());
            int isChoose = 0;
            if(likeMapper.selectByUserIdAndCommentId(userId, a.getId()).size() != 0){
                isChoose = 1;
            }
            //根据 当前的评论id查询子评论的数量
            LambdaQueryWrapper<AttractionsComments> lambdaQueryWrappers = new LambdaQueryWrapper<>();
            lambdaQueryWrappers.eq(AttractionsComments::getParentId, a.getId());
            Long commentNum = attractionsCommentsMapper.selectCount(lambdaQueryWrappers);
            Long count = likeService.getCountByCommentId(a.getId());
            attractionsCommentsInfoDto.setAttractionsId(a.getAttractionsId());
            attractionsCommentsInfoDto.setContent(a.getContent());
            attractionsCommentsInfoDto.setImage(user.getImage());
            attractionsCommentsInfoDto.setCreatedAt(a.getCreatedAt());
            attractionsCommentsInfoDto.setName(user.getName());
            attractionsCommentsInfoDto.setLikeNum(count);
            attractionsCommentsInfoDto.setUserId(a.getUserId());
            attractionsCommentsInfoDto.setCommentId(a.getId());
            attractionsCommentsInfoDto.setIsChoose(isChoose);
            attractionsCommentsInfoDto.setCommentNum(commentNum);
            listInfo.add(attractionsCommentsInfoDto);
        }
        PageInfoDto<AttractionsCommentsInfoDto> pageInfoDto = new PageInfoDto<>();
        pageInfoDto.setPages(page.getPages());
        pageInfoDto.setPageCurrent(page.getCurrent());
        pageInfoDto.setPageTotal(page.getTotal());
        pageInfoDto.setSize(page.getSize());
        pageInfoDto.setList(listInfo);
        return pageInfoDto;
    }

    /**
     * 删除评论
     * @param id
     * @return
     */
    @Override
    public int deleteCommentById(Long id) {
        return attractionsCommentsMapper.deleteById(id);
    }

    /**
     * 回复评论
     * @return
     */
    @Override
    public int addChildComment(AttractionsCommentsDto attractionsCommentsDto) {
        AttractionsComments attractionsComments = AttractionsComments.builder()
                .userId(attractionsCommentsDto.getUserId())
                .attractionsId(attractionsCommentsDto.getAttractionsId())
                .content(attractionsCommentsDto.getContent())
                .parentId(attractionsCommentsDto.getParentId())
                .build();
        return attractionsCommentsMapper.insert(attractionsComments);
    }

    @Override
    public PageInfoDto<AttractionsCommentsInfoDto> getCommentByParentId(Long id, int pageNum, int pageSize) {
        Page<AttractionsComments> page = new Page<>(pageNum, pageSize);
        attractionsCommentsMapper.selectByParentId(page, id);
        List<AttractionsComments> list = page.getRecords();
        List<AttractionsCommentsInfoDto> listInfo = new ArrayList<>();
        for(AttractionsComments a : list){
            User user = new User();
            AttractionsCommentsInfoDto attractionsCommentsInfoDto = new AttractionsCommentsInfoDto();
            user = userMapper.selectById(a.getUserId());
            Long count = likeService.getCountByCommentId(a.getId());
            attractionsCommentsInfoDto.setAttractionsId(a.getAttractionsId());
            attractionsCommentsInfoDto.setContent(a.getContent());
            attractionsCommentsInfoDto.setImage(user.getImage());
            attractionsCommentsInfoDto.setCreatedAt(a.getCreatedAt());
            attractionsCommentsInfoDto.setName(user.getName());
            attractionsCommentsInfoDto.setParentId(a.getParentId());
            attractionsCommentsInfoDto.setLikeNum(count);
            attractionsCommentsInfoDto.setUserId(a.getUserId());
            attractionsCommentsInfoDto.setCommentId(a.getId());
            listInfo.add(attractionsCommentsInfoDto);
        }
        PageInfoDto<AttractionsCommentsInfoDto> pageInfoDto = new PageInfoDto<>();
        pageInfoDto.setPages(page.getPages());
        pageInfoDto.setPageCurrent(page.getCurrent());
        pageInfoDto.setPageTotal(page.getTotal());
        pageInfoDto.setSize(page.getSize());
        pageInfoDto.setList(listInfo);
        return pageInfoDto;
    }

    @Override
    public PageInfoDto<AttractionsCommentsInfoDto> getCommentByParentIdAndUserId(Long userId, Long id, int pageNum, int pageSize) {
        Page<AttractionsComments> page = new Page<>(pageNum, pageSize);
        attractionsCommentsMapper.selectByParentId(page, id);
        List<AttractionsComments> list = page.getRecords();
        List<AttractionsCommentsInfoDto> listInfo = new ArrayList<>();
        for(AttractionsComments a : list){
            User user = new User();
            AttractionsCommentsInfoDto attractionsCommentsInfoDto = new AttractionsCommentsInfoDto();
            user = userMapper.selectById(a.getUserId());
            int isChoose = 0;
            if(likeMapper.selectByUserIdAndCommentId(userId, a.getId()).size() != 0){
                isChoose = 1;
            }
            Long count = likeService.getCountByCommentId(a.getId());
            attractionsCommentsInfoDto.setAttractionsId(a.getAttractionsId());
            attractionsCommentsInfoDto.setContent(a.getContent());
            attractionsCommentsInfoDto.setImage(user.getImage());
            attractionsCommentsInfoDto.setCreatedAt(a.getCreatedAt());
            attractionsCommentsInfoDto.setName(user.getName());
            attractionsCommentsInfoDto.setParentId(a.getParentId());
            attractionsCommentsInfoDto.setLikeNum(count);
            attractionsCommentsInfoDto.setUserId(a.getUserId());
            attractionsCommentsInfoDto.setCommentId(a.getId());
            attractionsCommentsInfoDto.setIsChoose(isChoose);
            listInfo.add(attractionsCommentsInfoDto);
        }
        PageInfoDto<AttractionsCommentsInfoDto> pageInfoDto = new PageInfoDto<>();
        pageInfoDto.setPages(page.getPages());
        pageInfoDto.setPageCurrent(page.getCurrent());
        pageInfoDto.setPageTotal(page.getTotal());
        pageInfoDto.setSize(page.getSize());
        pageInfoDto.setList(listInfo);
        return pageInfoDto;
    }

    @Override
    public Long getAllCommentNum(Long attractionsId) {
        LambdaQueryWrapper<AttractionsComments> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AttractionsComments::getAttractionsId, attractionsId);
        return attractionsCommentsMapper.selectCount(lambdaQueryWrapper);
    }


}
